// pages/index_detail/yuyue/yuyue.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr:[],
    originalArr:[],
    arr1:[],
    originalArr1:[],
    selectedOption:"",
    selectedOption1:"",
    date: '2016-09-01',
    time: '12:01',
    datatime:'',
    situation: '',

  },

  showActionSheet: function() {
    let that = this;
    wx.showActionSheet({
      itemList: this.data.arr, // 显示name数组
      success: function(res) {
        if (!res.cancel) {
          let selectedName = that.data.arr[res.tapIndex];
          let selectedLevel = that.data.originalArr.find(item => item.name === selectedName);
          if (selectedLevel) {
            let selectedLevelId = selectedLevel.levelId;
            that.setData({
              selectedOption: selectedName // 更新选中的name
            });
            wx.setStorageSync('selectedLevelId', selectedLevelId); // 保存对应的levelId
            console.log(selectedName, selectedLevelId); // 打印被点击的name和对应的levelId
          } else {
            console.error('未找到对应的levelId');
          }
        } else {
          console.log('用户点击取消');
        }
      },
      fail: function() {
        console.log('API调用失败');
      }
    });
  },
  bindPickerChange: function(e) {
    const index = e.detail.value; // 获取选择的索引
    const selectedSpecializationName = this.data.arr1[index]; // 获取选择的咨询类别名称
    const selectedSpecialization = this.data.originalArr1.find(item => item.specializationName === selectedSpecializationName);
    
    if (selectedSpecialization) {
      const selectedSpecializationId = selectedSpecialization.specializationId;
      this.setData({
        selectedOption1: selectedSpecializationName // 更新页面显示的咨询类别名称
      });
      wx.setStorageSync('selectedSpecializationId', selectedSpecializationId); // 保存对应的specializationId
      console.log('选择的咨询类别名称:', selectedSpecializationName, '对应的ID:', selectedSpecializationId);
    } else {
      console.error('未找到对应的咨询类别ID');
    }
  },
  bindDateChange: function(e) {
    const date = e.detail.value;
    this.setData({
      date: date,
      datatime: `${date} ${this.data.time}`
    });
    wx.setStorageSync('datatime', this.data.datatime);
    console.log('选择的日期:', date, '拼接后的日期时间:', this.data.datatime);
  },
  bindTimeChange: function(e) {
    const time = e.detail.value;
    this.setData({
      time: time,
      datatime: `${this.data.date} ${time}`
    });
    wx.setStorageSync('datatime', this.data.datatime);
    console.log('选择的时间:', time, '拼接后的日期时间:', this.data.datatime);
  },
  bindTextAreaInput: function(e) {
    const situation = e.detail.value;
    this.setData({
      situation: situation
    });
    wx.setStorageSync('situation', situation);
    console.log('情况说明:', situation);
  },
  enterYuyueDetail(){
    wx.navigateTo({
      url: '../yuyue_detail/yuyue_detail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.GetLevels();
    this.GetSpecialization();

  },

 
  GetLevels:function(){
    let that = this;
    wx.request({
      url: 'http://localhost:8085/level/findAllLevel',
      method:'GET',
      success(res){
        console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            arr: res.data.data.map(item => item.name),
            originalArr:res.data.data,
          });
        } else {
          wx.showToast({ title: '获取失败', icon: 'none' });
        }
      },
      fail() {
        wx.showToast({ title: '请求失败', icon: 'none' });
      }
    })
  },
  GetSpecialization:function(){
    let that = this;
    wx.request({
      url: 'http://localhost:8085/specialization',
      method:'GET',
      success(res){
        console.log(res.data.data);
        if (res.data.data) {
          that.setData({
            arr1: res.data.data.map(item => item.specializationName),
            originalArr1:res.data.data,
          });
        } else {
          wx.showToast({ title: '获取失败', icon: 'none' });
        }
      },
      fail() {
        wx.showToast({ title: '请求失败', icon: 'none' });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})